'''
Created on Aug 10, 2010

@author: oabalbin
'''
import numpy as np
import rpy2.robjects as robjects
import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr

from optparse import OptionParser
from collections import deque, defaultdict

def list_of_names_in_line_dictionary(inputfile):     

    genlist=defaultdict()
    for line in inputfile:        
        line = line.strip('\n')
        genlist0 = line.split('\t')
        #print genlist0[0], genlist0[1:]
        genlist[genlist0[0]]=genlist0[2:]
    
    return genlist    

def list_of_names_in_line(inputfile):     

    genlist=deque()
    for line in inputfile:        
        line = line.strip('\n')
        genlist = line.split('\t')
            
    return genlist


def list_of_names(inputfile):
    
    genlist=deque()
    for line in inputfile:        
        line = line.strip('\n')
        fields = line.split('\t')
        #print fields 
        # To skip headers, star reading samples in column 7 of the file
        if fields[0][0] == '#': 
            continue                             
        if fields[0]=='':
            continue
        else:
            genlist.append(fields[0])
    
    return genlist

def gene_sets_intersection(mygeneset1, mygeneset2, external_geneset=[]):
    
    if external_geneset:
        first_set =  set(mygeneset1).intersection(set(external_geneset))
        second_set = set(mygeneset2).intersection(set(external_geneset))
    else:
        cmp = set(mygeneset1).intersection(mygeneset2)
        print cmp
        return cmp
    print "Transcription set"
    print first_set
    print "phosphorylation set"
    print second_set
    print "trans-phospho intersection set"
    print first_set.intersection(second_set)

    return first_set, second_set






if __name__ == '__main__':
    
    optionparser = OptionParser("usage: %prog [options] ")
    optionparser.add_option("-f", "--mygenset", dest="mygenset",
                            help="mygenset file to use")
    optionparser.add_option("-u", "--universe_geneset", dest="universe_geneset",
                            help="universe_geneset file to use")
    optionparser.add_option("-c", "--cancer_geneset", dest="cancer_geneset",
                            help="external_sets file to use")    
    optionparser.add_option("-s", "--external_sets", dest="external_sets",
                            help="external_sets file to use")

    (options, args) = optionparser.parse_args()

    mygeneset=list_of_names(open(options.mygenset))
    universe_geneset=list_of_names(open(options.universe_geneset))
    cancer_genes = list_of_names(open(options.cancer_geneset))
    external_sets=list_of_names_in_line_dictionary(open(options.external_sets))
    
    
    for set_name, gene_set  in external_sets.iteritems():
        print set_name 
        first_set, second_set = gene_sets_intersection(mygeneset, universe_geneset, gene_set)
        print "intersection with cancer genes"
        gene_sets_intersection(first_set, second_set, cancer_genes)

